import { useEffect, useState } from 'react';
import { cline, useCur, useWait } from '../../nui';

const cmd_uid = (r) => {
    let f = r.map(d => d.uid);
    f = f.join(",");
    return f;
};

const useCmd = (cload, nd, root=cline(), hindex=null) => {
    const [filter, setFilter] = useState("");
    const [cur, fin, hcur, hreturn] = useCur(nd);
    const [wait, wrong, startWait, endWait, setWrong] = useWait();
    const [data_main, setMain] = useState([]);

    useEffect(() => {
        setFilter("");
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [root, cload]);

    return {
        cur: cur,
        fin: fin,
        hcur: hcur,
        hreturn: (v=true, hok=null) => {
            hreturn();
        },
        wait: wait,
        wrong: wrong,
        startWait: startWait,
        endWait: endWait,
        setWrong: setWrong,
        data: data_main,
        setMain: setMain,
        filter: filter,
        setFilter: setFilter,
    };
};

export {cmd_uid, useCmd, cline};
